cc.Class({
    extends: cc.Component,

    properties: {
       spSkin:cc.Sprite,
       spBj:cc.Sprite,
       btnSkin:cc.Button,
       shopNode:null,
       isCheck:false,
       typeSkin:null,
    },

    onLoad () {},

    setSkinData:function(data, panel, defaultSkin, showType){
        var url = "";
        this.spSkin.spriteFrame = data;
        this.shopNode = panel;
        this.typeSkin = showType;
        var isUnlock;
        if(showType == "ball")
        {
            this.spSkin.node.width = 200;
            this.spSkin.node.height = 200;
            isUnlock = window.gameConfig.getGameConfigData("ballUnlockArr");
        }
        else
        {
            this.spSkin.node.width = 339;
            this.spSkin.node.height = 152;
            isUnlock = window.gameConfig.getGameConfigData("ganUnlockArr");
        }
        
        var ballColor = null;
        if(isUnlock.length > 0)
        {
            let len = data.name.split("_");
            if(isUnlock.indexOf(len[1]) != -1)
            {
                if(defaultSkin == data.name)
                {
                    url = "check";
                    this.isCheck = true;
                }
                else
                {
                    url = "uncheck";
                }
                ballColor = new cc.color(255,255,255,255)
            }
            else
            {
                url = "uncheck";
                ballColor = new cc.color(63,63,63,255)
            }
        }

        this.spSkin.node.color = ballColor;

        this.onLoadRes(url);
    },

    onLoadRes: function (url) {
        var self = this;
        cc.loader.loadRes("image/gameLevel/" + url, cc.SpriteFrame, function (err, spriteFrame) {
            self.spBj.spriteFrame = spriteFrame;
        });
    },

    //按钮回调
    onButtonShopChangeSkin:function(){
        window.audioMgr.prototype.playButtonAudio();
        var isUnlock = window.gameConfig.getGameConfigData("ballUnlockArr");
        let ballName = this.spSkin.spriteFrame.name;
        let len = ballName.split("_");
        if(isUnlock.indexOf(len[1]) == -1)return;
        this.setOtherItemUnCheck();
        this.onLoadRes("check");
        if(this.typeSkin == "ball")
        {
            window.gameConfig.setDefualtBallSkin(this.spSkin.spriteFrame.name);
        }
        else
        {
            window.gameConfig.setTangGanSkin(this.spSkin.spriteFrame.name);
        }
        this.shopNode.setDefaultBallSkin(this.spSkin.spriteFrame.name)
    },


    //解锁点亮当前皮肤
    unlockSkin:function(){
        this.spSkin.node.color = new cc.color(255,255,255,255);
        this.setOtherItemUnCheck();
        this.onLoadRes("check");
        let ballName = this.spSkin.spriteFrame.name;
        let len = ballName.split("_");
        let skin;
        //把皮肤添加到已解锁列表中
        if(len[0] == "ball")
        {
            skin = window.gameConfig.getGameConfigData("ballUnlockArr")
            window.gameConfig.setDefualtBallSkin(this.spSkin.spriteFrame.name);
            let newArr = skin + len[1] + ","
            window.gameConfig.setBallUnlockArr(newArr);
        }
        else
        {
            skin = window.gameConfig.getGameConfigData("ganUnlockArr");
            let newArr = skin + len[1] + ","
            window.gameConfig.setTangGanSkin(this.spSkin.spriteFrame.name);
            window.gameConfig.setGanUnlockArr(newArr);
        }
        this.shopNode.setDefaultBallSkin(this.spSkin.spriteFrame.name);

        //扣除钻石
        window.gameConfig.gold = window.gameConfig.gold - 300;
        window.gameConfig.setGoldNumber(window.gameConfig.gold)
    },

    //查找当前皮肤是否已经解锁了  是返回false  没有返回true
    isUnlockSkin:function(){
        let ballName = this.spSkin.spriteFrame.name;
        let len = ballName.split("_");
        if(len[0] == "ball")
        {
            let skin = window.gameConfig.getGameConfigData("ballUnlockArr")
            if(skin.indexOf(len[1]) == -1)return true;
            else return false;
        }
        else
        {
            let skin = window.gameConfig.getGameConfigData("ganUnlockArr");
            if(skin.indexOf(len[1]) == -1)return true;
            else return false;
        }
    },

    //点亮的 改为未点亮
    setOtherItemUnCheck:function(){
        let item = this.shopNode.itemArr
        for(let i = 0; i < item.length; i++)
        {
            if(item[i].name == this.spSkin.spriteFrame.name)
            {
                item[i].isCheck = true;
                // item[i].onLoadRes("check");
            }
            else
            {
                item[i].isCheck = false;
                item[i].onLoadRes("uncheck");
            }
        }
    },
});
